package com.topcent.tioxyc.douyin.service;

/**
 * @author: 陈百万
 * @date: 2025/6/19 15:40
 * @Description: 同步物料信息到sap，wms，简道云
 */
// Java 版本：逐行读取产品数据并依次调用三个接口，同步到中台、SAP、WMS

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ProductSyncService {

    private static final String URL_0 = "http://125.91.116.95:9999/jeecg-boot/product/jdyProduct/saveJdyProductBatch";
    private static final String URL_SAP = "http://125.91.116.95:9999/jeecg-boot/product/jdyProduct/syncProductToSap";
    private static final String URL_WMS = "http://125.91.116.95:9999/jeecg-boot/product/jdyProduct/syncProduct2Wms";
    private static final String TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NjEyODgyNzYsInVzZXJuYW1lIjoiYWRtaW4ifQ.oUbUJ33lmNcdPxeUodn_Tm3D1rC6qWBcK5EF7E-EzF8";

    private static final String FILE_PATH = "products.txt";

    public static void main(String[] args) throws IOException {
        LocalDateTime start = LocalDateTime.now();
        System.out.println("程序开始执行，开始时间: " + formatTime(start));

        List<String> products = Files.readAllLines(Paths.get(FILE_PATH));
        List<String> failedProducts = new ArrayList<>();

        for (String product : products) {
            product = product.trim();
            if (product.isEmpty()) continue;
            System.out.println("正在处理 product: " + product);

            boolean step1 = post(URL_0, product, "中台", failedProducts);
            if (step1) {
                post(URL_SAP, product, "SAP", failedProducts);
                post(URL_WMS, product, "WMS", failedProducts);
            }
        }

        // 保存失败产品
        Files.write(Paths.get(FILE_PATH), failedProducts);

        LocalDateTime end = LocalDateTime.now();
        System.out.println("处理完成，未成功的产品已保存回文件。");
        System.out.println("程序执行结束，结束时间: " + formatTime(end));
        System.out.println("总运行时间: " + (java.time.Duration.between(start, end).toMillis() / 1000.0) + " 秒");
    }

    private static boolean post(String url, String product, String tag, List<String> failedProducts) {
        String payload = JSONUtil.createObj().set("product", product).toString();
        try (HttpResponse response = HttpRequest.post(url)
                .header("X-Access-Token", TOKEN)
                .body(payload)
                .execute()) {

            int code = response.getStatus();
            String body = response.body();
            if (code == 200) {
                System.out.println("成功处理 product: " + product + " 到" + tag + ", 响应: " + body);
                return true;
            } else {
                System.out.println("处理失败 product: " + product + " 到" + tag + ", 响应: " + body);
                failedProducts.add(product);
                return false;
            }
        } catch (Exception e) {
            System.out.println("异常处理 product: " + product + " 到" + tag + ", 错误: " + e.getMessage());
            failedProducts.add(product);
            return false;
        }
    }

    private static String formatTime(LocalDateTime time) {
        return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}

